package top.jle.locker.integration;

import lombok.Getter;
import org.noear.solon.Solon;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;
import top.jle.locker.annotation.OperationLock;
import top.jle.locker.aspect.OperationLockExecutorImpl;

@Getter
public class OperationLockInterceptor implements Interceptor {
    public static final OperationLockInterceptor instance = new OperationLockInterceptor();

    private static OperationLockExecutorImpl impl;

    static{
        Solon.context().getBeanAsync(OperationLockExecutorImpl.class, bean -> impl = bean);
    }

    @Override
    public Object doIntercept(Invocation inv) throws Throwable {
        OperationLock anno = inv.getMethodAnnotation(OperationLock.class);
        return impl
                .lock(anno, inv, inv::invoke);
    }
}
